//

import UIKit

class WP_CustomerViewController: WP_BasicViewController {
    
    var isPush:Bool = false
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        
         WP_CoreDataModel.coreDataManager.setEntityName(text: "CustomerModel")
        getData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavTitle("Customer")
        // Do any additional setup after loading the view.
        weak var weakSelf = self
        setupRightSetting(UIImage(named: "arrow39") as Any) {
            
            let vc = WP_CustomerAddViewController()
            weakSelf?.navigationController?.pushViewController(vc, animated: true)
            
        }
        self.view.addSubview(customerTableView)
    }
    
    
    lazy var customerTableView:UITableView = {
        
        let tableview = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - kNavBarHeight), style: .plain)
        tableview.delegate = self
        tableview.dataSource = self
        tableview.rowHeight = kRatioHeight(70)
        tableview.tableFooterView = UIView()
        tableview .register(WP_CustomerListTableViewCell.classForCoder(), forCellReuseIdentifier:NSStringFromClass(WP_CustomerListTableViewCell.classForCoder()))
        return tableview
        
    }()
    lazy var dataArray:[Any] = {
        let array = Array<Any>()
        return array
    }()
    
    
    func getData()  {
        
         WP_CoreDataModel.coreDataManager.readData(sequenceKeys:Array(), ascending: false, filterStr: nil, success: { (arr) in
            
            if arr.isEmpty && isPush == false {
                let vc = WP_CustomerAddViewController()
                self.navigationController?.pushViewController(vc, animated: true)
                isPush = true
                return
            }
            self.dataArray = arr
            customerTableView.reloadData()
            
        }, fail: {})
        
    }
    
}

extension WP_CustomerViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:WP_CustomerListTableViewCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(WP_CustomerListTableViewCell.classForCoder()), for: indexPath) as! WP_CustomerListTableViewCell
        cell.selectionStyle = .none
        
        cell.setModel(model: dataArray[indexPath.row] as! CustomerModel)
        
        return cell
    }
    //*
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool{
        return true
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?{
        return "删除"
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
    {
        let model = dataArray[indexPath.row] as! CustomerModel
        print(model.iD)
        if editingStyle == .delete {
            
             WP_CoreDataModel.coreDataManager.deleteData(key: "iD = \(model.iD)", success: {getData()}, fail: {})
        }
        
    }
    //*/
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let model = dataArray[indexPath.section] as! CustomerModel
        let vc = WP_CustomerDetailViewController()
        vc.model = model
        vc.ID = model.iD
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}
